<!DOCTYPE html>
<html class="x-admin-sm">
{include file='public/header'}
<script src="/admin/xadmin/lib/layui/upload.js" charset="utf-8"></script>
<style>
    .layui-form-label {
        width: 120px;
    }

    .layui-input-block {
        margin-left: 150px;
    }

    .juset {
        display: flex;
        align-items: center;
        flex-direction: row;
    }

    .back_rate_type2 {
        display: flex;
        align-items: center;
        flex-direction: row;
    }

    .layui-input-block {
        margin-left: 165px;
    }

    #selectAccount .layui-input-block {
        margin-left: 0;
    }

    .designated_amount_echo {
        width: 50%;
    }

    .designated_amount_echo .item {
        display: flex;
        justify-content: space-between;
        margin-top: 10px;
        height: 38px;
        width: 400px;
    }

    .designated_amount_echo .item div {
        display: flex;
        align-items: center;
    }

    .designated_amount_echo .item .content div {
        margin-right: 10px;
    }

    .designated_amount_echo .item input {
        flex: 1;
        width: 75px;
        height: 30px;
    }

    .designated_amount_echo .item .operate span {
        margin-right: 15px;
        display: inline-block;
        width: 20px;
        height: 20px;
        padding: 0;
        line-height: 20px;
    }

    .layui-form-label {
        width: 135px;
    }

    .fairy-tag {
        display: flex;
    }

    .fairy-tag-container {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
    }
</style>

<body>
    <div class="x-body">
        <form class="layui-form" lay-filter="formAjax">

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>回传订单比例
                </label>
                <div class="layui-input-block">
                    <input type="radio" name="back_rate_type" lay-filter="back_rate_type" value="0" title="百分比例"
                        checked>
                    <input type="radio" name="back_rate_type" lay-filter="back_rate_type" value="1" title="循环比例">
                </div>

                <div class="back_rate_type1">
                    <input type="number" name="echo_rate" required="nums" style="width: 80px;"
                        class="layui-input payamount" onblur="validateInput(this)" />
                </div>
                <div style="display: none;" class="back_rate_type2">
                    <div style="display: flex;">
                        <input type="number" name="pass" style="width: 80px;" class="layui-input pass"
                            onblur="validateInput(this)" />：<input type="number" name="leak" style="width: 80px;"
                            class="layui-input leak" onblur="validateInput(this)" />
                    </div>
                </div>
            </div>




            <input type="hidden" name="link_id" id="link_id" value="" />
            <button id="submitData" class="layui-btn" lay-submit lay-filter="submitData" style="display: none;" />

        </form>
    </div>
    <script>
        let params = {
            // addUrl: '/cps/back/add',
            editUrl: '/cps/back/editEchoRate',
            detailUrl: '/cps/back/showEchoData',
            hasCallback: false,
        }
        form_ajax(params)

        layui.use('form', function () {
            var form = layui.form;

            form.verify({
                nums: [
                    /^([1-9][0-9]{0,1}|100)$/, '只能输入1-100的整数'
                ],

                designated_amount_echo: function (value, item) {
                    if (value && form.val("formAjax").design_switch == 1) {
                        let designated_amount_echo = JSON.parse(value);
                        let priceList = []
                        for (let index = 0; index < designated_amount_echo.length; index++) {
                            const element = designated_amount_echo[index];
                            if (element.price == '' || element.rate == '') {
                                return '指定金额回传的金额或回传率不能为空';
                            }
                            priceList.push(element.price);
                        }
                        // if (isRepeat(priceList))
                        //     return '指定金额回传存在金额一样的数据';



                    }

                }

            });

            form.on('submit(submitData)', function (data) {
                // console.log(data);
                var formData = data.field
                let not_echo_recharge_price = parseInt(formData.not_echo_recharge_price)
                let leak = parseInt(formData.leak)
                let pass = parseInt(formData.pass)
                if (formData.back_rate_type == 0 && formData.echo_rate == '') {
                    layer.msg('回传订单比例不能为空')
                    return false
                } else if (formData.back_rate_type == 1 && formData.leak == '' && formData.pass == '' && leak < 0 && pass < 0) {
                    // if(){
                    layer.msg('循环比例不能为空,并且不能小于0')
                    return false
                } else if (formData.is_open_low_recharge_not_echo == 1 && formData.not_echo_recharge_price == '' || not_echo_recharge_price < 0) {
                    layer.msg('低冲不会回传不能为空,并且不能小于0')
                    return false
                } else {
                    formData.is_new_back_setting = is_new_back_setting
                    formData.link_id = link_id
                    formReloadAjax('/cps/back/editEchoRate', formData)
                    return false
                }
                return false
            });


            form.on('radio(back_rate_type)', function (data) {

                if (data.value == 1) {
                    $('.back_rate_type2').css('display', 'block');
                    $('.back_rate_type1').css('display', 'none');
                } else {
                    $('.back_rate_type1').css('display', 'block');
                    $('.back_rate_type2').css('display', 'none');

                }
            });

            form.on('radio(design_switch)', function (data) {
                $('.designated_amount_echo').css('display', data.value === '1' ? 'block' : 'none')
            });
            form.on('radio(is_open_low_recharge_not_echo)', function (data) {

                if (data.value == 0) {
                    $('.is_open').hide()
                } else {
                    $('.is_open').show()
                }
            });
        });

        function validateInput(input) {


            // 判断输入值不能为空
            if (input.value == "") {
                layer.msg('输入不能为空')

                return;
            }

            // 判断输入值不能小于0
            if (input.value < 0) {
                layer.msg('请输入大于0的数字')

                return;
            }

        }

        let link_id = GetQueryString('link_id')
        let is_new_back_setting;
        if (link_id) {

            Ajax(params.detailUrl, { link_id }).then(result => {

                is_new_back_setting = result.data.is_new_back_setting
                if (result.data.back_rate_type == 1) {
                    $('.back_rate_type2').show()
                    $('.back_rate_type1').hide()
                } else {
                    $('.back_rate_type1').show()
                    $('.back_rate_type2').hide()

                }

                layui.use(["form", "layer"], function () {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;
                    form.val("formAjax", result.data);
                });



            });
        }

        // 添加回传金额



    </script>
</body>

</html>